package org.example;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;

public class Config {

    //接收udp数据端口
    private Integer appUdpPort;
    //接收tcp数据端口
    private  Integer appTcpPort;
    //魔兽争霸3端口
    private Integer war3Port;

    public static Config instance;

    static {
        Properties prop = new Properties();
        FileInputStream fileInputStream = null;
        InputStream inputStream = null;
        try {

            fileInputStream = new FileInputStream("config.properties");

        } catch (Exception ignored) {

        }

        try {
            if (fileInputStream != null) {
                prop.load(fileInputStream);
            } else {
                inputStream = Config.class.getClassLoader().getResourceAsStream("config.properties");
                prop.load(inputStream);
            }

            Integer  appUdpPort = Integer.valueOf(prop.getProperty("app.udp.port"));
            Integer appTcpPort = Integer.valueOf(prop.getProperty("app.tcp.port"));
            Integer war3Port = Integer.valueOf(prop.getProperty("war3.port"));

            instance = new Config(appUdpPort, appTcpPort, war3Port);;
            System.out.println("config.properties 配置文件加载成功。");
            System.out.println(instance);

        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("config.properties 配置文件加载异常");
        }finally {

        if(fileInputStream!=null){
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if(inputStream!=null){
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    }

    public Config(Integer appUdpPort, Integer appTcpPort, Integer war3Port) {
        this.appUdpPort = appUdpPort;
        this.appTcpPort = appTcpPort;
        this.war3Port = war3Port;
    }

    public Integer getAppUdpPort() {
        return appUdpPort;
    }

    public Integer getAppTcpPort() {
        return appTcpPort;
    }

    public Integer getWar3Port() {
        return war3Port;
    }

    @Override
    public String toString() {
        return "Config{" +
                "appUdpPort=" + appUdpPort +
                ", appTcpPort=" + appTcpPort +
                ", war3Port=" + war3Port +
                '}';
    }
}
